You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

[...nextauth].ts 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { NextApiRequest, NextApiResponse } from 'next'
  2. import NextAuth from 'next-auth'
  3. import Providers from 'next-auth/providers'
  4. export default function (req: NextApiRequest, res: NextApiResponse) {
  5. return NextAuth(req, res, {
  6. providers: [
  7. Providers.GitHub({
  8. clientId: process.env.GITHUB_ID,
  9. clientSecret: process.env.GITHUB_SECRET,
  10. }),
  11. ],
  12. callbacks: {
  13. async redirect(url, baseUrl) {
  14. return url.startsWith(baseUrl) ? url : baseUrl
  15. },
  16. async session(session, token) {
  17. // @ts-ignore
  18. session.user.id = token.id
  19. return session
  20. },
  21. async signIn(user, account, profile) {
  22. // @ts-ignore
  23. const canLogin = await isSponsoringMe(profile?.login)
  24. if (canLogin) {
  25. return canLogin
  26. } else {
  27. return '/sponsorware'
  28. }
  29. },
  30. },
  31. })
  32. }
  33. const whitelist = ['steveruizok']
  34. async function isSponsoringMe(login: string) {
  35. if (whitelist.includes(login)) return true
  36. const res = await fetch('https://api.github.com/graphql', {
  37. method: 'POST',
  38. headers: {
  39. 'Content-Type': 'application/json',
  40. Authorization: 'bearer ' + process.env.GITHUB_API_SECRET,
  41. },
  42. body: JSON.stringify({
  43. query: `
  44. query {
  45. user(login: "steveruizok") {
  46. isSponsoredBy(accountLogin: "${login}")
  47. }
  48. }
  49. `,
  50. }),
  51. }).then((res) => res.json())
  52. return res?.data?.user?.isSponsoredBy
  53. }